package com.itheima.reggin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggin.common.R;
import com.itheima.reggin.entity.Employee;
import com.itheima.reggin.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 员工管理
 * @author
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;
    /**
     * 员工登录，需要验证账户，浏览器记录，退出登录清除
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {

        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(lambdaQueryWrapper);
        if (emp == null) {
            return R.error("此账户不存在！");
        }
        if (!emp.getPassword().equals(password)) {
            R.error("密码错误！");
        }
        if (emp.getStatus() == 0) {
            R.error("此账户处于禁用状态！");
        }
        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    /**
     * 退出登录
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        log.info("员工退出");
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 增加
     */
    @PostMapping()
    public R<String> save(@RequestBody Employee employee) {
        log.info("保存新员工");
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employeeService.save(employee);
        return R.success("新员工增加成功！");
    }

    /**
     * 查询
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(!StrUtil.isEmpty(name), Employee::getName, name);
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        employeeService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    /**
     *查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id) {
        log.info("查询员工信息方法");
        Employee employee = employeeService.getById(id);
        if (employee != null) {
            return R.success(employee);
        }
        return R.error("没有查询到对应员工信息");
    }
    /**
     *改
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> updata(@RequestBody Employee employee) {
        log.info("修改");
        employeeService.updateById(employee);
        return R.success("修改成功");
    }
    /**
     * 删除
     */
    @DeleteMapping
    public R<String> delete(Long id){
        boolean b = employeeService.removeById(id);
        if(b){
            return R.success("删除成功");
        }
        return R.error("delete error!!");
    }

}
